//
//  TextViewController.m
//  HeSaid
//
//  Created by ZC on 15/6/3.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "TextViewController.h"
#import "AppDelegate.h"
#import "SetUpViewController.h"
#define feedbackURL @"http://app.jiwaipro.com:8888/hesaid/index.php/HeSaid/feedback"


@interface TextViewController (){
    AppDelegate *app;
    NSString *feedBackStr;
    UIAlertView *_alert;
    UIButton *doneButton;
    int keyboard;
}

@end

@implementation TextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textBack.layer.cornerRadius = 8;
    self.textBack.layer.borderColor = [UIColor grayColor].CGColor;
    self.textBack.layer.borderWidth = 0.5;
    app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    doneButton =  [[UIButton alloc]init];
    [doneButton setBackgroundImage:[UIImage imageNamed:@"done.png"] forState:UIControlStateNormal];
    [doneButton addTarget: self action:@selector(hideKeyboard) forControlEvents: UIControlEventTouchUpInside];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (doneButtonshow:) name: UIKeyboardDidShowNotification object:nil];
    
    // Do any additional setup after loading the view from its nib.
}
-(void) doneButtonshow: (NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [value CGRectValue];
    keyboard = keyboardRect.size.height;
    doneButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-30, [UIScreen mainScreen].bounds.size.height-(keyboard+85), 30, 20);
    [self.view addSubview:doneButton];
}
-(void) hideKeyboard {
    [doneButton removeFromSuperview];
    [self.textBack resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)submitButton:(id)sender {
        if ([self.textBack.text isEqualToString:@""]) {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入内容" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }else{
        NSString *poststr =[NSString stringWithFormat:@"feedback_phone=%@&feedback_content=%@",app.user_phone,self.textBack.text];
        [self londingPath:feedbackURL Str:poststr];
    
        }
}

-(void)londingPath:(NSString *)path Str:(NSString *)string{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        feedBackStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:feedBackStr waitUntilDone:YES];
    }];
}
-(void)refreshData:(NSMutableArray *)sender{
    int str=[feedBackStr intValue];
    if (str == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提交失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提交成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        _alert=alert;
        [alert show];
    }
}

#pragma mark - 提示框“确定”事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0&&alertView==_alert) {
        //跳转到设置页面
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        SetUpViewController *setUp = [storyboard instantiateViewControllerWithIdentifier:@"setUpVC"];
        [self.navigationController pushViewController:setUp animated:YES];
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    doneButton.hidden = YES;
    [self.textBack resignFirstResponder];
}
@end
